package com.mobile.check.activitys;


import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import cn.droidlover.xdroidmvp.kit.NToast;
import com.mobile.check.app.R;

public class ControlEquipmentActivity extends BaseCommActivity implements OnClickListener {
    private static final String DETECTOR_FID = "detector fid";
    private static final String DETECTOR_PID = "detector pid";
    private static final String IGNITE_FID = "ignite";
    private static final String LOG_START = "log start";
    private static final String PUMP_OFF = "pump off";
    private static final String PUMP_ON = "pump on";
    private static final String PUSH_1 = "push 1";
    private static final String PUSH_EX = "push ex";
    private static final String QUIT = "quit";
    private static final String SCREEN = "screen";
    private Handler mHandler;
    private ProgressDialog mProgressDialog;
    private boolean mbHideSendArea;
    private RelativeLayout mrlSendArea;
    private String msEndFlg;
    private ScrollView msvRecView;
    private ScrollView msvSendView;
    private TextView mtvRecAreaTitle;
    private TextView mtvRecView;
    private TextView mtvSendView;

    public ControlEquipmentActivity() {
        this.msEndFlg = msEND_FLGS[0];
        this.mtvSendView = null;
        this.mtvRecView = null;
        this.mtvRecAreaTitle = null;
        this.msvSendView = null;
        this.msvRecView = null;
        this.mrlSendArea = null;
        this.mbHideSendArea = false;
        this.mHandler = new Handler();
    }

    private void Send(String var1) {
        if (!var1.equals("")) {
            if (var1.equals("log start") && this.mProgressDialog != null && this.mProgressDialog.isShowing()) {
                this.mProgressDialog.dismiss();
            }

            int var2;
            if (!this.msEndFlg.isEmpty()) {
                var2 = this.mBSC.Send(var1.concat(this.msEndFlg));
            } else {
                var2 = this.mBSC.Send(var1);
            }

            if (var2 >= 0) {
                if (this.mrlSendArea.getVisibility() == 0) {
                    if (var2 == 0) {
                        this.mtvSendView.append(var1.concat("(fail) "));
                    } else {
                        this.mtvSendView.append(var1.concat("(succeed) "));
                    }
                }
            } else {
                Toast.makeText(this, this.getString(R.string.msg_msg_bt_connect_lost), 1).show();
                this.mtvRecView.append(this.getString(R.string.msg_msg_bt_connect_lost) + "\n");
            }

            this.refreshTxdCount();
            this.autoScroll();
        }

    }

    private void autoScroll() {
        int var1 = this.mtvRecView.getMeasuredHeight() - this.msvRecView.getHeight();
        if (var1 > 0) {
            this.msvRecView.scrollTo(0, var1);
        }

        if (this.mrlSendArea.getVisibility() == 0) {
            var1 = this.mtvSendView.getMeasuredHeight() - this.msvSendView.getHeight();
            if (var1 > 0) {
                this.msvSendView.scrollTo(0, var1);
            }
        }

    }

    private void sendCombinationOrders(String[] var1, int[] var2) {
        int var3 = 0;
        this.mProgressDialog = ProgressDialog.show(this, (CharSequence)null, "正在发送指令", false, false);
        int var4 = 0;

        for(int var5 = var1.length; var3 < var5; ++var3) {
            final String var6 = var1[var3];
            this.mHandler.postDelayed(new Runnable() {
                public void run() {
                    Send(var6);
                }
            }, (long)var2[var4]);
            ++var4;
        }

    }

    private void setEndFlg() {
        this.mBSC.setReceiveStopFlg(this.msEndFlg);
    }

    public void onClick(View var1) {
        switch(var1.getId()) {
            case 2131230782:
                this.Send("detector fid");
                return;
            case 2131230783:
                this.Send("detector pid");
                return;
            case 2131230784:
                this.sendCombinationOrders(new String[]{"screen", "push 1", "quit", "log start"}, new int[]{0, 1000, 6000, 7000});
                return;
            case 2131230800:
                this.Send("pump off");
                return;
            case 2131230801:
                this.Send("pump on");
                return;
            case 2131230803:
                this.sendCombinationOrders(new String[]{"screen", "push ex", "quit", "log start"}, new int[]{0, 200, 2200, 2400});
                return;
            default:
        }
    }

    public void onConfigurationChanged(Configuration var1) {
        super.onConfigurationChanged(var1);
    }

    protected void onCreate(Bundle var1) {
        super.onCreate(var1);
        this.setContentView(R.layout.activity_control_equipment);
        this.mtvSendView = (TextView)this.findViewById(R.id.actKeyBoard_tv_send_data_show);
        this.mtvRecView = (TextView)this.findViewById(R.id.actKeyBoard_tv_receive_show);
        this.mtvRecAreaTitle = (TextView)this.findViewById(R.id.actKeyBoard_tv_receive_area_title);
        this.msvSendView = (ScrollView)this.findViewById(R.id.actKeyBoard_sv_send_data_scroll);
        this.msvRecView = (ScrollView)this.findViewById(R.id.actKeyBoard_sv_receive_data_scroll);
        this.mrlSendArea = (RelativeLayout)this.findViewById(R.id.actKeyBoard_rl_send_area);
        this.mtvRecView.setText("");
        this.mtvRecAreaTitle.append("\t\t(");
        this.mtvRecAreaTitle.append(this.getString(R.string.tips_click_to_hide));
        this.mtvRecAreaTitle.append(":" + this.getString(R.string.tv_send_area_title));
        this.mtvRecAreaTitle.append(")");
        this.mtvRecAreaTitle.setOnClickListener(new OnClickListener() {
            public void onClick(View var1) {
                boolean var2 = false;
                if (var1.getId() == R.id.actKeyBoard_tv_receive_area_title) {
                    String var3 = getString(R.string.tv_receive_area_title);
                    TextView var4 = (TextView)var1;
                    if (mbHideSendArea) {
                        var3 = var3 + "\t\t(" + getString(R.string.tips_click_to_hide);
                        var4.setText(var3 + ":" + getString(R.string.tv_send_area_title) + ")");
                        mrlSendArea.setVisibility(View.VISIBLE);;
                    } else {
                        var3 = var3 + "\t\t(" + getString(R.string.tips_click_to_show);
                        var4.setText(var3 + ":" + getString(R.string.tv_send_area_title) + ")");
                        mrlSendArea.setVisibility(View.GONE);
                    }

                    mrlSendArea.refreshDrawableState();
                    ControlEquipmentActivity var5 = ControlEquipmentActivity.this;
                    if (!mbHideSendArea) {
                        var2 = true;
                    }

                    var5.mbHideSendArea = var2;
                }

            }
        });
        String var3 = this.getString(R.string.tv_receive_area_title);
        var3 = var3 + "\t\t(" + this.getString(R.string.tips_click_to_show);
        var3 = var3 + ":" + this.getString(R.string.tv_send_area_title) + ")";
        this.mtvRecAreaTitle.setText(var3);
        this.mrlSendArea.setVisibility(View.GONE);
        this.mrlSendArea.refreshDrawableState();
        this.mbHideSendArea = true;
        this.findViewById(R.id.btn_ignite_fid).setOnClickListener(this);
        this.findViewById(R.id.btn_pump_on).setOnClickListener(this);
        this.findViewById(R.id.btn_pump_off).setOnClickListener(this);
        this.findViewById(R.id.btn_reboot).setOnClickListener(this);
        this.findViewById(R.id.btn_detector_fid).setOnClickListener(this);
        this.findViewById(R.id.btn_detector_pid).setOnClickListener(this);

        try {
            this.enabledBack();
            this.initIO_Mode();
            this.usedDataCount();
            this.setEndFlg();
            (new ControlEquipmentActivity.receiveTask()).executeOnExecutor(FULL_TASK_EXECUTOR, new String[0]);
        } catch (NullPointerException var2) {
            NToast.showToast( this,"请先连接设备",1);
            this.finish();
        }
    }

    public boolean onCreateOptionsMenu(Menu var1) {
        super.onCreateOptionsMenu(var1);
        var1.add(0, 1, 0, this.getString(R.string.menu_clear)).setShowAsAction(1);
        return super.onCreateOptionsMenu(var1);
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.mBSC != null) {
            this.mBSC.killReceiveData_StopFlg();
        }

    }

    public boolean onKeyDown(int var1, KeyEvent var2) {
        if (4 == var1) {
            this.mbThreadStop = true;
            this.setResult(0, (Intent)null);
            this.finish();
            return true;
        } else {
            return super.onKeyDown(var1, var2);
        }
    }

    public boolean onOptionsItemSelected(MenuItem var1) {
        switch(var1.getItemId()) {
            case 1:
                this.mtvSendView.setText("");
                this.mtvRecView.setText("");
                return true;
            case 16908332:
                this.mBSC.killReceiveData_StopFlg();
                this.mbThreadStop = true;
                this.setResult(0);
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(var1);
        }
    }

    private class receiveTask extends AsyncTask<String, String, Integer> {
        private static final int CONNECT_LOST = 1;
        private static final int THREAD_END = 2;

        private receiveTask() {
        }

        protected Integer doInBackground(String... var1) {
            mBSC.Receive();

            while(!mbThreadStop) {
                if (!mBSC.isConnect()) {
                    return 1;
                }

                SystemClock.sleep(10L);
                if (mBSC.getReceiveBufLen() > 0) {
                    this.publishProgress(new String[]{mBSC.Receive()});
                }
            }

            return 2;
        }

        public void onPostExecute(Integer var1) {
            if (1 == var1) {
                mtvRecView.append(getString(R.string.msg_msg_bt_connect_lost));
            } else {
//                mtvRecView.append(getString(R.string.msg_receive_data_stop));
            }

            refreshHoldTime();
        }

        public void onPreExecute() {
            mtvRecView.append(getString(R.string.msg_receive_data_wating));
            mbThreadStop = false;
        }

        public void onProgressUpdate(String... var1) {
            mtvRecView.append(var1[0]);
            autoScroll();
            refreshRxdCount();
        }
    }
}
